/**
 * author: zhou
 * date: 2017.10.23
 * description: 学生管理中心
 * functions: {
 *      save: 保存学生信息
 *      findAll: 获取全部学生
 *      findOne: 获取学生信息
 *      update: 更新学生
 * }
 * 
 */

const   mongoPromise = require('../../../core/mongo/mongoPromise'),
        Student      = require('../../../core/mongo/models/school/studentModel');

const studentController = {
    /**
     * 保存学生信息
     * @param {*} ctx 
     */
    async save(ctx) {
        const { name, studentId, schoolId, gradeId, classId, idCard, birthday, sex, parentName, parentPhone } = ctx.request.body.data;
        if (!name) {
            ctx.body = { success: false, message: '学生姓名不能为空', state: 10004 };
            return;
        }
        if (!studentId) {
            ctx.body = { success: false, message: '学生学号不能为空', state: 10004 };
            return;
        }
        if (!schoolId) {
            ctx.body = { success: false, message: '学校id不能为空', state: 10004 };
            return;
        }
        if (!gradeId) {
            ctx.body = { success: false, message: '年级id不能为空', state: 10004 };
            return;
        }
        if (!classId) {
            ctx.body = { success: false, message: '班级id不能为空', state: 10004 };
            return;
        }
        if (!idCard) {
            ctx.body = { success: false, message: '学生身份证号码不能为空', state: 10004 };
            return;
        }
        if (!birthday) {
            ctx.body = { success: false, message: '学生出生日期不能为空', state: 10004 };
            return;
        }
        if (!parentName) {
            ctx.body = { success: false, message: '学生家长姓名不能为空', state: 10004 };
            return;
        }
        if (!parentPhone) {
            ctx.body = { success: false, message: '学生家长手机号不能为空', state: 10004 };
            return;
        }
        const student = new Student({
            name,
            studentId,
            schoolId,
            gradeId,
            classId,
            idCard,
            birthday,
            sex: Number(sex),
            parentName,
            parentPhone
        });
        await Student.create(student).then((doc) => {
            ctx.body = { success: true, data: [], state: 200 };
        },
        (error) => {
            console.log(error);
            ctx.body = { success: false, message: '抱歉,保存学生失败', state: 10003 };
        });
    },
    /**
    * 获取班级列表
    * @param {*} ctx 
    */
    async findAll(ctx) {
        const { schoolId, gradeIds, classIds, date, limit } = ctx.request.body.data;
        if (!schoolId) {
            ctx.body = { success: false, message: '学校id不能为空', state: 10004 };
            return;
        }
        const queryParam = {
            schoolId,
            status: 1
        };
        if (gradeIds) {
            queryParam.gradeId = { $in: gradeIds };
        }
        if (classIds) {
            queryParam.classId = { $in: classIds };
        }
        if (date) {
            queryParam.createDate = { $lt: date };
        }
        let query = Student.find(queryParam).sort({ createDate: -1 });
        if (limit) {
            query = query.limit(limit);
        }
        query = query.populate('gradeId', { _id: 0, name: 1 }).populate('classId', { _id: 0, name: 1 });
        const result = await mongoPromise(ctx, query);
        if (result) {
            const queryParam = {
                schoolId,
                status: 1
            };
            if (gradeIds) {
                queryParam.gradeId = { $in: gradeIds };
            }
            if (classIds) {
                queryParam.classId = { $in: classIds };
            }
            const countQuery = Student.count(queryParam);
            const countResult = await mongoPromise(ctx, countQuery);
            if (countResult !== undefined) {
                ctx.body = { success: true, data: { data: result, count: countResult }, state: 200 };
            }
        }
    },
    /**
    * 查询班级信息
    * @param {*} ctx 
    */
    async findOne(ctx) {
        const { id } = ctx.request.body.data;
        if (!id) {
            ctx.body = { success: false, message: '学生id不能为空', state: 10004 };
            return;
        }
        const query = Student.findById(id);
        const result = await mongoPromise(ctx, query);
        if (result !== undefined) {
            ctx.body = { success: true, data: result, state: 200 };
        } else {
            ctx.body = { success: false, message: '抱歉,未查找到该学生', state: 200 };
        }
    },
    /**
     * 更新班级信息
     * @param {*} ctx 
     */
    async update(ctx) {
        const { _id, name, studentId, schoolId, gradeId, classId, idCard, birthday, sex, parentName, parentPhone } = ctx.request.body.data;
        if (!_id) {
            ctx.body = { success: false, message: '学生id不能为空', state: 10004 };
            return;
        }
        if (!name) {
            ctx.body = { success: false, message: '学生姓名不能为空', state: 10004 };
            return;
        }
        if (!studentId) {
            ctx.body = { success: false, message: '学生学号不能为空', state: 10004 };
            return;
        }
        if (!schoolId) {
            ctx.body = { success: false, message: '学校id不能为空', state: 10004 };
            return;
        }
        if (!gradeId) {
            ctx.body = { success: false, message: '年级id不能为空', state: 10004 };
            return;
        }
        if (!classId) {
            ctx.body = { success: false, message: '班级id不能为空', state: 10004 };
            return;
        }
        if (!idCard) {
            ctx.body = { success: false, message: '学生身份证号码不能为空', state: 10004 };
            return;
        }
        if (!birthday) {
            ctx.body = { success: false, message: '学生出生日期不能为空', state: 10004 };
            return;
        }
        if (!parentName) {
            ctx.body = { success: false, message: '学生家长姓名不能为空', state: 10004 };
            return;
        }
        if (!parentPhone) {
            ctx.body = { success: false, message: '学生家长手机号不能为空', state: 10004 };
            return;
        }
        const student = {
            name,
            studentId,
            schoolId,
            gradeId,
            classId,
            idCard,
            birthday,
            sex: Number(sex),
            parentName,
            parentPhone
        };
        const query = Student.update({ _id }, { $set: student });
        const result = await mongoPromise(ctx, query);
        if (result) {
            ctx.body = { success: true, data: [], state: 200 };
        }
    },
    /**
     * 删除学生
     * @param {*} ctx 
     */
    async del(ctx) {
        const { id } = ctx.request.body.data;
        if (!id) {
            ctx.body = { success: false, message: '学生id不能为空', state: 10004 };
            return;
        }
        const query = Student.update({ _id: id }, { $set: { status: 2 }});
        const result = await mongoPromise(ctx, query);
        if (result) {
            ctx.body = { success: true, data: [], state: 200 };
        }
    }
};

module.exports = studentController;